package com.fufan.mtsaas.modules.home.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.home.bo.request.HomeQueryRequest;
import com.fufan.mtsaas.modules.home.bo.response.HomeMyApply;
import com.fufan.mtsaas.modules.home.bo.response.HomeSysMessage;
import com.fufan.mtsaas.modules.home.bo.response.HomeTotal;
import com.fufan.mtsaas.modules.home.service.HomeService;
import com.fufan.mtsaas.modules.newtech.xjstech.dao.XjsTechDao;
import com.fufan.mtsaas.modules.newtech.xjsyearaudit.bo.response.XjsYearAuditResponse;
import com.fufan.mtsaas.modules.newtech.xjsyearaudit.dao.XjsYearAuditDao;
import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service(value = "homeServiceImpl")
@Slf4j
@Primary
public class HomeServiceImpl implements HomeService {

    /**
     * 数据库操作
     */
    @Autowired
    private XjsTechDao xjsTechDao;


    public Result<HomeTotal> total(){
        Result<HomeTotal> result = new Result<>();
        HomeTotal homeTotal = new HomeTotal();

        RequestPojo requestPojo = ParamUtil.doChange();
        Long id = Long.valueOf(requestPojo.getSysId());
        Page page = new Page();
        // 已上报
        long applyTotal = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                xjsTechDao.myApply(page, id);
            }
        });
        homeTotal.setApplyTotal(applyTotal);
        // 未审批
        long noAuditTotal = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                xjsTechDao.noAuditList(page, id);
            }
        });
        homeTotal.setNoAuditTotal(noAuditTotal);
        // 已审批
        long auditTotal = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                xjsTechDao.auditList(page, id);
            }
        });
        homeTotal.setAuditTotal(auditTotal);

        result.setResData(page.setRecords(Arrays.asList(homeTotal)));
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;
    }

    /**
     * 首页-我上报的
     * @return
     */
    public Result<HomeMyApply> myApply(HomeQueryRequest mapData){
        Result<HomeMyApply> result = new Result<>();
        RequestPojo requestPojo = ParamUtil.doChange();
        Long id = Long.valueOf(requestPojo.getSysId());
        Page page = new Page();
        // 分页
        ParamUtil.setPage(mapData.getPageSize(), mapData.getCurrentPage(), page);
        List<HomeMyApply> applyList = xjsTechDao.myApply(page, id);
        result.setResData(page.setRecords(applyList));
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;
    }

    /**
     * 首页-待审批列表
     * @return
     */
    public Result<HomeMyApply> noAuditList(HomeQueryRequest mapData){
        Result<HomeMyApply> result = new Result<>();
        RequestPojo requestPojo = ParamUtil.doChange();
        Long id = Long.valueOf(requestPojo.getSysId());
        Page page = new Page();
        // 分页
        ParamUtil.setPage(mapData.getPageSize(), mapData.getCurrentPage(), page);
        List<HomeMyApply> applyList = xjsTechDao.noAuditList(page, id);
        result.setResData(page.setRecords(applyList));
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;
    }

    public Result<HomeSysMessage> sysMessageList(){
        Result<HomeSysMessage> result = new Result<>();
        List<HomeSysMessage> applyList = new ArrayList<>();
        HomeSysMessage message = new HomeSysMessage();
        message.setId(111);
        message.setName("您有一条新的不安全事件审批任务");
        message.setTime(new Date());
        message.setState(0);
        //TODO 待实现

        Page page = new Page();
        result.setResData(page.setRecords(applyList));
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;
    }
}
